//
//  TDLocationView.m
//  TFB
//
//  Created by yull on 15/8/15.
//  Copyright (c) 2015年 TD. All rights reserved.
//

#import "TDLocationView.h"
#import "TDProvince.h"

@interface TDLocationView () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UILabel *titleL;
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray *cityList;
@property (nonatomic, strong) NSArray *areaList;

@property (nonatomic, strong) TDProvince *province;
@property (nonatomic, strong) TDCity *city;
@property (nonatomic, strong) TDArea *area;

@end

@implementation TDLocationView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:0.2 alpha:0.3];
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 30, frame.size.width - 20, frame.size.width - 60)];
        view.backgroundColor = [UIColor whiteColor];
        view.layer.cornerRadius = 3;
        view.layer.masksToBounds = YES;
        
        UILabel *titleL = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, view.frame.size.width, 40)];
        titleL.font = [UIFont systemFontOfSize:18];
        titleL.textAlignment = NSTextAlignmentCenter;
        [view addSubview:titleL];
        self.titleL = titleL;
        
        UIView *lineV = [[UIView alloc] initWithFrame:CGRectMake(0, 40, view.frame.size.width, 1)];
        lineV.backgroundColor = [UIColor grayColor];
        [view addSubview:lineV];
        
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, view.frame.size.width, view.frame.size.height - 40) style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        [view addSubview:tableView];
        // iOS 7
        if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
            [tableView setSeparatorInset:UIEdgeInsetsZero];
        }
        // iOS 8
        if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
            [tableView setLayoutMargins:UIEdgeInsetsZero];
        }
        self.tableView = tableView;
        
        [self addSubview:view];
    }
    
    return self;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:UIEdgeInsetsZero];
        
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.areaList.count) {
        return self.areaList.count;
    }
    
    if (self.cityList.count) {
        return self.cityList.count;
    }
    
    return self.locations.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"LocationIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    if (self.areaList.count) {
        TDArea *area = self.areaList[indexPath.row];
        cell.textLabel.text = area.areaName;
    } else if (self.cityList.count) {
        TDCity *city = self.cityList[indexPath.row];
        cell.textLabel.text = city.cityName;
    } else {
        TDProvince *province = self.locations[indexPath.row];
        cell.textLabel.text = province.provName;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!self.cityList.count) {
        
        TDProvince *province = self.locations[indexPath.row];
        self.cityList = province.cityList;
        [tableView reloadData];
        
        self.province = province;
        
    } else if (!self.areaList.count) {
        
        TDCity *city = self.cityList[indexPath.row];
        self.areaList = city.areaList;
        [tableView reloadData];
        
        self.city = city;
        
    } else {
        
        TDArea *area = self.areaList[indexPath.row];
        
        self.area = area;
        
        if (self.complete) {
            self.complete(self.province, self.city, self.area);
        }
    }
}

@end
